package com.inventory.api.controller;

import com.inventory.api.dao.ProductDao;
import com.inventory.api.entity.Product;
import com.inventory.api.exception.MyException;
import com.inventory.api.service.ProductService;
import com.inventory.api.signUpReq.InvntSignupReq;
import com.inventory.api.util.Result;
import com.inventory.api.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2018/1/29.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/inventory", produces = "application/json;charset=utf-8")
public class ProductController {


    @Autowired
    ProductService productService;

    @Autowired
    ProductDao productDao;


    /**
     * 商品入库
     *
     * @param signUpReq
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/stock-in")
    public ResponseEntity stockIn(@RequestBody InvntSignupReq signUpReq) {
        try {
            productService.stockIn(signUpReq);
            return new ResponseEntity(new Result(200), HttpStatus.OK);
        } catch (MyException ex) {
            log.error("stockIn is error=>signUpReq:{}", signUpReq, ex);
            return new ResponseEntity(new Result(400, ex.getMessage()), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("stockIn is error=>signUpReq:{}", signUpReq, ex);
            return new ResponseEntity(new Result(500), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 商品出库
     *
     * @param signUpReq
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/stock-out")
    public ResponseEntity stoctOut(@RequestBody InvntSignupReq signUpReq) {
        try {
            return new ResponseEntity(productService.stockOut(signUpReq), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("stoctOut is error=>signUpReq:{}", signUpReq, ex);
            return new ResponseEntity(new Result(500), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 获取商品信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/products")
    public ResponseEntity getProducts(@RequestBody List<String> ids) {
        List<ProductVo> vos = new ArrayList<>();

        List<Product> products = productDao.findByIdIn(ids);
        for (Product product : products) {
            ProductVo productVo = new ProductVo(product.getId(), product.getWarehouseQuantity());
            vos.add(productVo);
        }
        Map map = new HashMap();
        map.put("products", vos);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    /**
     * 获取商品信息
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/products")
    public ResponseEntity getProducts(@RequestParam(value = "storeId") String storeId) {
        Map map = new HashMap();
        map.put("products", productDao.findByStoreId(storeId));
        return new ResponseEntity(map, HttpStatus.OK);
    }


}
